<?php
//模块名_控制名
class User_PositionController extends Zend_Controller_Action
{

	public function preDispatch(){
		$this->tplDirectory = "user/position/";	//控制对应模板目录
	}
	
	public function indexAction(){
		
		$departmentArr = User_Service_Department::getAll();		//部门列表
		$total = User_Service_Position::getByCond(array(),'count(*)','up_id', 0, 0);
		
		$this->view->total = $total;
		$this->view->departmentArr = $departmentArr;
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");	
    }
    
    public function listAction(){
    	$lang = Zend_Registry::get('lang');
    	$page = $this->_request->getParam('page',1);
    	$pageSize = $this->_request->getParam('pageSize',20);
    	$page = $page?$page:1;
    	$up_name = $this->_request->getParam('up_name','');
    	$cond = array();
    	if(!empty($up_name)){
    		$cond['up_name'] = $up_name;
    	}
   
		$orderby='up_id';
		$result = User_Service_Position::getByCond($cond,'*',$orderby, $pageSize, ($page - 1)*$pageSize);		
		$total = User_Service_Position::getByCond($cond,'count(*)',$orderby, 0, 0);
		foreach($result as $key => $value){
			$Department=User_Service_Department::getById($value['ud_id']);
			$result[$key]['ud_id']=$lang['current_language']=='cn'? $Department['ud_name']: $Department['ud_en_name'];
		}
		$data=array('ask'=>0);
		if($total>0){
			$data=array(
				'total'=>$total,
				'result'=>$result,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
    }
    
    public function editAction(){
    	$up_id = $this->_request->getParam('up_id','');
    	$up_name = $this->_request->getParam('up_name','');
    	$ud_id = $this->_request->getParam('ud_id','');
    	$up_level = $this->_request->getParam('up_level','');
    	
    	$row = array(
			'up_name' => $up_name,
			'ud_id' => $ud_id,
			'up_level' => $up_level,
		);
		
		if($up_id){
			$result = User_Service_Position::update($row,$up_id);			
		}else {
			$result = User_Service_Position::add($row);
		}
		if($result){
			$msg = array( 'ask'=>1 );
    	}else{
    		$msg = array( 'ask'=>0 ,'error'=>array('error!'),);
    	}
		echo json_encode($msg);	
    }
    
    public function deleteAction(){
    	$up_id = $this->_request->getParam('id','');
    	if(!empty($up_id)){
    		$result = User_Service_Position::delete($up_id);
    		if($result){
    			$msg = array( 'ask'=>1 );
    		}else{
    			$msg = array( 'ask'=>0 );
    		}
    		echo json_encode($msg);
    	}

    }
    
    public function singleAction(){
    	$up_id = $this->_request->getParam('id','');
    	if($up_id){
    		$result = User_Service_Position::getByUpid($up_id);
    		if( count($result) ){
				$data=array(
					'result'=>$result,
					'ask'=>1
				);
			}
			die(Zend_Json::encode($data));
    	}
    }
    
    public function rightsAction(){
    	$act = $this->_request->getParam('act','');
    	$id = $this->_request->getParam('id','');	
    	
    	if($act != 'loadData'){    		  
			$result = User_Service_PositionRightMap::getByPid($id);
			$actionIdArr  = array();
			foreach ($result as $k =>$v){
				$actionIdArr[] = $v['ur_id'];
			}
			
			$this->view->up_id = $id;
			$this->view->rightIdArr = implode(',',$actionIdArr);
    		echo Cff::renderTpl($this->tplDirectory."rights.tpl","layoutaction");
    		exit();
    	}
    	$data=array('ask'=>0);
    	$total = User_Service_Right::getByCond(array(),'count(*)','ur_id',0,0);
    	if($total>0){			
			$result = User_Service_Right::getByCond(array(),'*','ur_module',0,0);	
			$data=array(
				'total'=>$total,
				'result'=>$result,
				'ask'=>1
			);
			
		}
    	die(Zend_Json::encode($data)); 	
    }
    
	public function saverightsAction(){
  		$delRow = $insertRow = $oldIdArr =  array();
    	$rights = $this->_request->getParam('rights',array());	//新提交的权限ID
    	$position_id = $this->_request->getParam('position_id','');
    	if(empty($position_id)){
    		exit('save error.');
    	}

		$result = User_Service_PositionRightMap::getByPid($position_id);
		foreach ($result as $key => $val){
			$oldIdArr[] = $val['ur_id'];		//已有的权限ID
		}
		$delIdArr = array_diff($oldIdArr,$rights);
		$insertIdArr = array_diff($rights,$oldIdArr);
		foreach ($delIdArr as $key => $val){
			$cond = array('position_id'=>$position_id,'ur_id'=>$val);
			$delRow[] = User_Service_PositionRightMap::deleteBy($position_id,$val);
		}
		
		foreach ($insertIdArr as $key => $val){
			$cond = array('position_id'=>$position_id,'ur_id'=>$val);
			$insertRow[] = User_Service_PositionRightMap::add($cond);
		}
		$data=array('ask'=>0);
		if(count($delRow) || count($insertRow)){
			$data=array('ask'=>1);			
		}
		die(Zend_Json::encode($data));
		
    }
        	
}